package servlet;

import base.BaseServlet;
import base.Result;
import biz.HomePageService;
import com.google.gson.Gson;
import pojo.Company;
import pojo.Job;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "HomePageServlet", urlPatterns = "/home")
public class HomePageServlet extends BaseServlet {
    HomePageService service = HomePageService.start();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        super.doPost(req, resp);
        if (param == null) return;
        try (PrintWriter writer = resp.getWriter()) {
            switch (param) {
                case "search":
                    if (jsonRequest.get("content") != null){
                        writer.write(service.search(jsonRequest.get("content").getAsString()));
                    }
                    break;
                case "hot":
                    writer.write(bean2Json(service.getHotSearchList()));
                    break;
                case "domain":
                    Thread.sleep(1000);
                    writer.write(bean2Json(service.getDomains()));
                    break;
                case "hotJobList":
                    writer.write(bean2Json(service.getJobList(1, -1)));
                    break;
                case "newJobList":
                    writer.write(bean2Json(service.getJobList(2, -1)));
                    break;
                case "jobList":
                    writer.write(bean2Json(service.getJobList(3, jsonRequest.get("page") != null ? jsonRequest.get("page").getAsInt() : null)));
                    break;
                case "companyList":
                    writer.write(bean2Json(service.getCompanyList(3, jsonRequest.get("page") != null ? jsonRequest.get("page").getAsInt() : null)));
                    break;
                case "jobTipsList":
                    writer.write(bean2Json(service.getJobTipsList(jsonRequest.get("industry") != null ? jsonRequest.get("industry").getAsString() : "")));
                    break;
                case "industries":
                    writer.write(bean2Json(service.getIndustries()));
                    break;
                case "addJob":
                    Thread.sleep(1000);
                    Job job = new Gson().fromJson(jsonRequest, Job.class);
                    if (job != null) {
                        job.setjCreateTime(System.currentTimeMillis());
                    }
                    writer.write(bean2Json(service.createJob(job)));
                    break;
                case "addCompany":
                    Thread.sleep(1000);
                    Company company = new Gson().fromJson(jsonRequest, Company.class);
                    writer.write(bean2Json(service.createCompany(company)));
                    break;
                case "exceptJop"://按条件筛选工作
                    if (jsonRequest.get("condition") != null){
                        service.search(jsonRequest.get("condition").getAsString());
                    }
                    writer.write(bean2Json(service.getExceptJobList(jsonRequest.get("condition") != null ? jsonRequest.get("condition").getAsString() : "",
                            jsonRequest.get("page") != null ? jsonRequest.get("page").getAsInt() : null)));
                    break;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            try (PrintWriter writer = resp.getWriter()) {
                Result<String> result = new Result<>();
                result.setCode(-1);
                result.setMessage("请求太频繁，请稍后重试！");
                result.setData("请求太频繁，请稍后重试！");
                writer.write(bean2Json(result));
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        service.closeSession();
    }
}
